import { createStore } from 'vuex'
import { useRouter } from 'vue-router'

const store = createStore({
    state: {
        removeRoutes: []
    },
    getters: {
        removeRoutes: state => state.removeRoutes
    },
    mutations: {
        SET_REMOVE_ROUTES(state, val) {
            state.removeRoutes = val
        }
    },
    actions: {
        addRoutes({ commit }, accessRoutes) {
            // 添加动态路由，同时保存移除函数，将来如果需要重置路由可以用到它们
            const removeRoutes = []
            const router = useRouter()
            accessRoutes.forEach(route => {
                const removeRoute = router.addRoute(route)
                removeRoutes.push(removeRoute)
            })
            commit('SET_REMOVE_ROUTES', removeRoutes)
        },
    }
})

export default store